﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text; 
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace VideoNetApplication.Common.Helper
{
 public   class DBHelper
 {
     /// <summary>
     ///// 返回DATATABLE
     ///// </summary>
     ///// <param name="sql">sql</param>
     ///// <param name="db">数据库连接</param>
     ///// <param name="parameters">参数数组</param>
     ///// <returns></returns>
     //public static DataTable ExecuteDataSet(string sql, Database db, List<Parameter> parameters)
     //{
     //    DataTable dataTable = null;
     //    using (DbCommand dbCommand = db.GetSqlStringCommand(sql))
     //    {
     //        BuildePrm(parameters, dbCommand, db);
     //        try
     //        {
     //            dataTable = db.ExecuteDataSet(dbCommand).Tables[0];

     //        }
     //        catch (Exception ex)
     //        {

     //             throw ;
     //        }


     //    }
     //    return dataTable;

     //}


     //public static DataSet RtnExecuteDataSet(string sql, Database db, List<Parameter> parameters)
     //{
     //    DataSet ds = null;
     //    using (DbCommand dbCommand = db.GetSqlStringCommand(sql))
     //    {
     //        BuildePrm(parameters, dbCommand, db);
     //        try
     //        {
     //            ds = db.ExecuteDataSet(dbCommand);

     //        }
     //        catch (Exception ex)
     //        {


     //            throw ;
     //        }
     //    }
     //    return ds;

     //}
        private static Database db = DatabaseFactory.CreateDatabase("VideoConnString");

       
        /// <summary>
        /// 返回datatable 不带参数
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public static DataSet ExecuteDataSet(string sql)
        {
            DataSet ds = null;
            using (DbCommand dbCommand = db.GetSqlStringCommand(sql))
            {
                ds = db.ExecuteDataSet(dbCommand);
            }
            return ds;

        }
        ///// <summary>
        ///// 返回执行查询的记录条数 
        ///// </summary>
        ///// <param name="sql"></param>
        ///// <param name="db"></param>
        ///// <param name="parameters"></param>
        ///// <returns></returns>
        //public static int ExecuteNonQuery(string sql, Database db, List<Parameter> parameters)
        //{
        //    int recordcount = 0;
        //    using (DbCommand dbCommand = db.GetSqlStringCommand(sql))
        //    {
        //        BuildePrm(parameters, dbCommand, db);
        //        try
        //        {
        //            recordcount = db.ExecuteNonQuery(dbCommand);
        //        }
        //        catch (Exception ex)
        //        {
 
        //            throw ex;
        //        }

        //    }
        //    return recordcount;
        //}


        /// <summary>
        /// 返回记录条数  不带参数数组
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string sql)
        {
            int recordcount = 0;
            using (DbCommand dbCommand = db.GetSqlStringCommand(sql))
            {
                try
                {
                    recordcount = db.ExecuteNonQuery(dbCommand);
                }
                catch (Exception ex)
                {
 
                    throw;
                }

            }
            return recordcount;
        }

        public static object ExecuteScalar(string sql)
        {
            object obj = null;
            using (DbCommand dbCommand = db.GetSqlStringCommand(sql))
            {
                try
                {
                    obj = db.ExecuteScalar(dbCommand);
                }
                catch (Exception ex)
                { 
                    throw ex;


                }

            }
            return DataHelper.GetInteger(obj);
        }

        /// <summary>
        /// 返回自己需要的类型的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static T ExecuteScalar<T>(string sql)
        {
            object obj = null;
            using (DbCommand dbCommand = db.GetSqlStringCommand(sql))
            {
                try
                {
                    obj = db.ExecuteScalar(dbCommand);
                }
                catch (Exception ex)
                {
                    throw ex;


                }

            }
            return (T)obj;
        }
        //public static object ExecuteScalar(string sql, Database db, List<Parameter> parameters)
        //{
        //    object obj = null;
        //    using (DbCommand dbCommand = db.GetSqlStringCommand(sql))
        //    {
        //        BuildePrm(parameters, dbCommand, db);
        //        try
        //        {
        //            obj = db.ExecuteScalar(dbCommand);
        //        }
        //        catch (System.Exception ex)
        //        { 
        //            throw ex;
        //        }

        //    }
        //    return DataHelper.GetInteger(obj);
        //}

        ///// <summary>
        ///// 组合参数
        ///// </summary>
        ///// <param name="parameters"></param>
        ///// <param name="dbCommand"></param>
        ///// <param name="db"></param>
        //public static void BuildePrm(List<Parameter> parameters, DbCommand dbCommand, Database db)
        //{
        //    foreach (var parameter in parameters)
        //    {
        //        db.AddInParameter(dbCommand, parameter.ValuePrm, parameter.DType, parameter.Value);
        //    }
        //}
    }
}

